package com.ocom.company.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.company.PersonCountVo;
import com.ocom.common.vo.company.PersonnelInfoVo;
import com.ocom.company.dao.IDaoPersonnelInfo;
import com.ocom.company.service.PersonnelInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<IDaoPersonnelInfo, EntityPersonnelInfo> implements PersonnelInfoService {


    @Autowired
    private IDaoPersonnelInfo iDaoPersonnelInfo;

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityPersonnelInfo getPerInfo(Long companyId, Long pId) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("p_id","p_code","p_name","dept_id");
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("status",0);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<PersonCountVo> selectPerCount(Long companyId) {
        return iDaoPersonnelInfo.selectPerCount();
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public Integer selectCount(Long companyId) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public PersonnelInfoVo getPersonnelInfoByPid(Long companyId, Integer pid) {
        QueryWrapper<Object> wrapper  = new QueryWrapper<>();
        wrapper.eq("P.com_id",companyId)
                .eq("P.p_id",pid)
                .eq("D.com_id",companyId)
                .eq("P.status",0)
                .orderByDesc("P.p_id")
                .last("limit 1");
        List<PersonnelInfoVo> personnelInfoVoList = this.baseMapper.getPersonnelInfo(wrapper);
        if(ObjectUtil.isEmpty(personnelInfoVoList)){
            return null;
        }else{
            return personnelInfoVoList.get(0);
        }
    }
}
